package krustyproject;

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.ArrayList;

import krustyproject.PalletPane.ActionHandler;

public class BlockedPalletPane extends BasicPane {
	private static final long serialVersionUID = 1;

	private JLabel currentUserNameLabel;

	private DefaultListModel nameListModel;

	private JList nameList;

	private DefaultListModel palletListModel;

	private JList palletList;

	private JTextField[] fields;

	private static final int PALLET_ID = 0;

	private static final int DATE = 1;

	private static final int COOKIE = 2;

	private static final int LOCATION = 3;

	private static final int ORDER_ID = 4;

	private static final int STATUS_ID = 5;

	private static final int NBR_FIELDS = 6;

	public BlockedPalletPane(Database db) {
		super(db);
	}

	public JComponent createLeftPanel() {
		nameListModel = new DefaultListModel();

		nameList = new JList(nameListModel);
		nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		nameList.setPrototypeCellValue("123456789012");
		nameList.addListSelectionListener(new NameSelectionListener());
		JScrollPane p1 = new JScrollPane(nameList);

		palletListModel = new DefaultListModel();

		palletList = new JList(palletListModel);
		palletList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		palletList.setPrototypeCellValue("123456789012");
		palletList.addListSelectionListener(new PalletSelectionListener());
		JScrollPane p2 = new JScrollPane(palletList);


		JPanel p = new JPanel();
		p.setLayout(new GridLayout(1, 2));
		p.add(p1);
		p.add(p2);
		return p;
	}

	public JComponent createTopPanel() {
		String[] texts = new String[NBR_FIELDS];
		texts[PALLET_ID] = "Pallet ID";
		texts[DATE] = "Date";
		texts[COOKIE] = "Cookie type";
		texts[LOCATION] = "Location";
		texts[ORDER_ID] = "Order";
		texts[STATUS_ID] = "Status";

		fields = new JTextField[NBR_FIELDS];
		for (int i = 0; i < fields.length; i++) {
			fields[i] = new JTextField(20);
			fields[i].setEditable(false);

		}
		
		

		JPanel input = new InputPanel(texts, fields);

		JPanel p1 = new JPanel();
		p1.setLayout(new FlowLayout(FlowLayout.LEFT));
		p1.add(new JLabel("Pallet information: "));
		currentUserNameLabel = new JLabel("");
		p1.add(currentUserNameLabel);

		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		p.add(p1);
		p.add(input);
		return p;
	}
	



	public void entryActions() {
		clearMessage();
		// currentUserNameLabel.setText(CurrentUser.instance().getCurrentUserId());
		fillNameList();
		fillPalletListAll();
		clearFields();
	}


	private void fillNameList() {
		nameListModel.removeAllElements();
		ArrayList<String> a = db.getCookies();
		for (int i = 0; i < a.size(); i++) {
			nameListModel.addElement(a.get(i));
		}
	}

	private void fillPalletList(String cookieName) {
		palletListModel.removeAllElements();
		ArrayList<Integer> a = db.getAllBlockedPalletIDByName(cookieName);
		for (int i = 0; i < a.size(); i++) {
			palletListModel.addElement(a.get(i));
		}
	}

	private void fillPalletListAll() {
		palletListModel.removeAllElements();
		ArrayList<Integer> a = db.getAllBlockedPalletID();
		for (int i = 0; i < a.size(); i++) {
			palletListModel.addElement(a.get(i));
		}
	}


	private void clearFields() {
		for (int i = 0; i < fields.length; i++) {
			fields[i].setText("");
		}
	}

	class NameSelectionListener implements ListSelectionListener {

		public void valueChanged(ListSelectionEvent e) {
			if (nameList.isSelectionEmpty()) {
				return;
			}
			palletList.clearSelection();
			String cookieName = (String) nameList.getSelectedValue();
			fields[0].setText("N/A");
			fields[1].setText("N/A");
			fields[2].setText("N/A");
			fields[3].setText("N/A");
			fields[4].setText("N/A");
			fields[5].setText("N/A");
			fillPalletList(cookieName);
		}

	}

	class PalletSelectionListener implements ListSelectionListener {

		public void valueChanged(ListSelectionEvent e) {
			if(palletList.isSelectionEmpty()){
				return;
			}
			int palletId = (Integer) palletList.getSelectedValue();

			fields[0].setText(palletList.getSelectedValue().toString());
			fields[1].setText(db.getPalletDate(palletId).toString());
			fields[2].setText(db.getPalletCookie(palletId).toString());
			fields[3].setText(db.getPalletLocation(palletId).toString());
			fields[4].setText(db.getPalletOrder(palletId).toString());
			fields[5].setText(db.getPalletStatus(palletId).toString());

		}
	}
	
	class ActionHandler implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			updateUI();

		}
	}
	
	

	

		
	}




